//
//  Meerkat.m
//  Return Of The Lemi
//
//  Created by Paul Vishayanuroj on 12/1/09.
//  Copyright 2009 Paul Vishayanuroj. All rights reserved.
//

#import "Meerkat.h"
#import "Layer_Creatures.h"

@implementation Meerkat

+ (id) meerkatWithPos:(Player *)player position:(CGPoint)pos
{
	return [[[self alloc] initWithPos:player position:pos] autorelease];
}

- (id) initWithPos:(Player *)player position:(CGPoint)pos
{
	if((self = [super init])) 
	{
		[self loadActions];
		
		[sprite setScale:CONST_MEERKAT_SCALE];
		
		[sprite setPosition:pos];
		
		_player = player;
		
		enemyState = idle;
		
		animateLock = NO;
		
		// Create the physics
		body = cpBodyNew(200.0f, INFINITY);
		
		body->p = cpv(pos.x, pos.y);
		
		cpVect verts[] = {
			cpv(-10, -43),
			cpv(-10, 30),
			cpv(-2, 30),
			cpv(-2, -43),
		};
		
		shape = cpPolyShapeNew(body, 4, verts, cpvzero);		
		shape->e = 0.9f; shape->u = 0.9f;
		shape->data = self;
		shape->collision_type = cTagEnemy; 
	}
	return self;
}

// Enemy override function
- (void) update
{
	CGFloat dist = [super distToPlayer];
	//NSLog(@"dist: %4.2f", dist);
	
	if (attackCooldownTimer > 0) attackCooldownTimer--;
	
	switch(enemyState)
	{
		case idle:
			if (dist < CONST_MEERKAT_ATTACK_RANGE)
			{
				enemyState = attacking;
			}
			break;
		case attacking:
			if (!animateLock && attackCooldownTimer == 0)
			{
				[sprite runAction: [Sequence actions: actionThrow, actionCreatePebble, actionUnlock, actionCooldown, nil]];
				animateLock = YES;
			}
			if (dist > CONST_MEERKAT_ATTACK_RANGE)
			{
				enemyState = idle;
			}
			break;
		default:
			break;
	}
}

- (CGPoint) getPosition
{
	return CGPointMake(sprite.position.x + CONST_MEERKAT_X_CENTER_OFFSET, sprite.position.y);
}

- (void) animateUnlock
{
	animateLock = NO;
}

- (void) attackCooldown
{
	attackCooldownTimer = CONST_MEERKAT_ATTACK_COOLDOWN;
}

- (void) firePebble
{
	[(Layer_Creatures *)spriteManager.parent fireMunition:pebble position:sprite.position];
}

- (void) loadActions
{
	spriteManager = [[AtlasSpriteManager spriteManagerWithFile: @"Meerkat_Spritesheet.png"] retain];
	
	// The default sprite
	sprite = [[AtlasSprite spriteWithRect:(CGRectMake(0, 0, CONST_FRAME_SIZE, CONST_FRAME_SIZE)) spriteManager:self.spriteManager] retain];
	
	// Create the animation objects with delays between each frame
	AtlasAnimation *animationThrow = [AtlasAnimation animationWithName: @"lemur_throw" delay: CONST_MEERKAT_THROW_ANIMATION_DELAY];
	
	// Add the throw animations from the sprite sheet
	for (int i = 0; i < CONST_MEERKAT_NUM_ATTACK_FRAMES; i++)
	{
		[animationThrow addFrameWithRect:(CGRectMake(i * CONST_FRAME_SIZE, 0, CONST_FRAME_SIZE, CONST_FRAME_SIZE))];
	}
	
	// Associate the animation with the sprite in case we need it later
	[sprite addAnimation: animationThrow];
	
	actionThrow = [[Animate actionWithAnimation: animationThrow] retain];
	actionUnlock = [[CallFunc actionWithTarget:self selector:@selector(animateUnlock)] retain];	
	actionCooldown = [[CallFunc actionWithTarget:self selector:@selector(attackCooldown)] retain];	 
	
	actionCreatePebble = [[CallFunc actionWithTarget:self selector:@selector(firePebble)] retain];  	
	
	[spriteManager addChild:sprite z:1 tag:kTagSprite];
}

- (void) dealloc
{
	[spriteManager release];
	[sprite release];
	[actionThrow release];
	[actionUnlock release];
	[actionCooldown release];
	[actionCreatePebble release];
	[super dealloc];
}

@end
